package com.ruoyi.web.controller.erp.goodPath;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.goodpath.domain.DeliveryRouteTab;
import com.ruoyi.erp.goodpath.service.GoodPathService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 2017.1.1
 * @Auther Du Ruifang @萌えキュンキュン
 * @Date 2020/12/11 13:29
 * @description 该类的作用
 */
@Api(tags = "交货路线")
@Controller
@RequestMapping("/goodsPath")
public class GoodsPathController extends BaseController {
    @Autowired
    private GoodPathService goodPathService;
    private String prefix = "erp/goodpath";
    @RequiresPermissions("goodsPath:goodpath:view")
    @GetMapping("/goodpath")
    public String list (){
        return prefix+"/list";
    }

    /**
     * 查询交货路线数据列表
     * @return 返回 goodsPathList
     */
    @RequiresPermissions("goodsPath:goodpath:list")
    @ApiOperation(value="查询列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo getList (DeliveryRouteTab deliveryRouteTab) {
        List<DeliveryRouteTab> deliveryRouteTabList = goodPathService.getList(deliveryRouteTab);
        return getDataTable(deliveryRouteTabList);
    }
    /**
     * 新增交货路线
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @RequiresPermissions("goodsPath:goodpath:add")
    @ApiOperation(value="添加交货路线")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult saveGoodPath (DeliveryRouteTab deliveryRouteTab){
        try {
            goodPathService.saveGoodPath(deliveryRouteTab);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("添加成功");
    }
    @ApiOperation(value="校验工艺路线标识")
    @PostMapping("/checkRouteId")
    @ResponseBody
    public String checkRouteId (String routeId) {
      String b=goodPathService.checkRouteId(routeId);
     return b;
    }


    /**
     * 查询交货路线
     */
    @ApiOperation(value="根据routeId查询详情")
    @GetMapping("/edit/{id}")
    public String findGoodPath(@PathVariable("id") String routeId, ModelMap mmap)
    {
        mmap.put("goodPath", goodPathService.findGoodPath(routeId));
        return prefix + "/edit";
    }
    /**
     * 修改保存交货路线
     */
    @RequiresPermissions("goodsPath:goodpath:edit")
    @PostMapping("/edit")
    @ApiOperation(value="修改保存交货路线")
    @ResponseBody
    public AjaxResult editSaveGoodPath(DeliveryRouteTab deliveryRouteTab){
        try {
            goodPathService.editSaveGoodPath(deliveryRouteTab);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return AjaxResult.error("修改保存失败");
        }
        return AjaxResult.success("修改保存成功");
    }

    @RequiresPermissions("goodsPath:goodpath:remove")
    @Log(title = "删除交货路线", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodPathService.deleteGoodPathByIds(ids));
    }

    @Log(title = "导出交货路线", businessType = BusinessType.EXPORT)
    @RequiresPermissions("goodsPath:goodpath:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DeliveryRouteTab deliveryRouteTab)
    {
        List<DeliveryRouteTab> deliveryRouteTabList = goodPathService.getList(deliveryRouteTab);
        ExcelUtil<DeliveryRouteTab> util = new ExcelUtil<DeliveryRouteTab>(DeliveryRouteTab.class);
        return util.exportExcel(deliveryRouteTabList, "交货路线数据");
    }
}
